<template>
  <a-modal
    title="记录信息"
    :visible="visible"
    :confirm-loading="loading"
    @ok="handleOk"
    @cancel="handleCancel"
    :afterClose="afterClose"
  >
    <a-form-model ref="form" :model="form" :rules="rules" :labelCol="{span:4}" :wrapperCol="{span:20}">
      <a-form-model-item has-feedback label="姓名" prop="name">
        <a-input v-model="form.name" autocomplete="off" />
      </a-form-model-item>
      <a-form-model-item has-feedback label="电话" prop="phone">
        <a-input v-model="form.phone" autocomplete="off" />
      </a-form-model-item>
      <a-form-model-item has-feedback label="事由" prop="reason">
        <a-input v-model="form.reason" autocomplete="off" />
      </a-form-model-item>
      <a-form-model-item has-feedback label="备注" prop="remark">
        <a-input v-model="form.reason" autocomplete="off" />
      </a-form-model-item>
      <a-form-model-item has-feedback label="工作地址" prop="workSite">
        <a-input v-model="form.workSite" autocomplete="off" />
      </a-form-model-item>
      <a-form-model-item has-feedback label="事由" prop="reason">
        <a-input v-model="form.reason" autocomplete="off" />
      </a-form-model-item>
      <a-form-model-item has-feedback label="厂区" prop="factoryId">
        <a-select v-model="form.factoryId" default-value="" @focus="getFactoryData">
          <a-select-option v-for="(item,index) in factorys" :key="index" :value="item.value">
            {{item.name}}
          </a-select-option>
        </a-select>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
  
</template>

<script>
import {getFactorysByName,savePerson} from "../../api/index";
export default {
  name:"RecordFormModal",
  props:{
    visible:Boolean,
    formData:Object
  },
  watch:{
    formData(newVal,oldVal){
      if(newVal&&newVal.id){
        this.form=newVal;
      }
    }
  },
  data(){
    return {
      loading:false,
      form:{
        name:"",
        phone:"",
        reason:"",
        remark:"",
        workSite:""
      },
      rules:{
        userName:[{ required: true, message: '请输入昵称', trigger: 'blur' },],
        accountNumber:[{ required: true, message: '请输入账号', trigger: 'blur' },],
        factoryId:[{ required: true, message: '请输入厂区', trigger: 'blur' },],
        password:[{ required: true, message: '请输入密码', trigger: 'blur' },]
      },
      factorys:[],
    }
  },
  created(){
    this.getFactoryData()
  },
  methods:{
    getFactoryData(){
      this.factorys=[]
      getFactorysByName().then(data =>{
        data.map(item =>{
          this.factorys.push({
            name:item.name,
            value:item.id
          })
        })
      })
    },
    afterClose(){
      this.$refs.form.resetFields();
      this.loading=false;
      this.$emit("update:formData",null);
      this.form={
        name:"",
        phone:"",
        reason:"",
        remark:"",
        workSite:""
      };
    },
    handleCancel(){
      this.$emit("update:visible",false);
    },
    handleOk(){
      this.$refs.form.validate(valid =>{
        if(valid){ 
          this.loading=true;
          savePerson(this.form).then(res =>{
            this.loading=false;
            this.$emit("update:visible",false);
            this.$emit("ok");
          }).catch(err =>{
            this.loading=false;
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>